package com.tsx.demo;

//创建 PdfAnnotation 对象
//该PdfAnnotation类的包com.itextpdf.kernel.pdf.annot代表的是所有注释的超类。
//在其派生类中，PdfLineAnnotation类表示线注释。创建此类的对象，如下所示
//设置注释的标题和内容
//分别使用PdfAnnotation类的setTitle()和setContents()方法设置注释的标题和内容，如下所示。

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation;
import com.itextpdf.layout.Document;

import java.nio.file.Paths;

public class LineAnnotation {
    public static void main(String[] args) throws Exception {
        // Creating a PdfWriter
        String dest = Paths.get(Constants.dest, "lineAnnotation.pdf").toString();
        PdfWriter writer = new PdfWriter(dest);

        // Creating a PdfDocument
        PdfDocument pdf = new PdfDocument(writer);

        // Creating a Document
        Document document = new Document(pdf);

        // Creating a PdfPage
        PdfPage page = pdf.addNewPage();

        // creating PdfLineAnnotation object
        Rectangle rect = new Rectangle(0, 0);
        float[] floatArray = new float[]{
                20, 790, page.getPageSize().getWidth() - 20, 790
        };
        PdfAnnotation annotation = new PdfLineAnnotation(rect, floatArray);

        // Setting color of the PdfLineAnnotation
        annotation.setColor(ColorConstants.BLUE);

        // Setting title to the PdfLineAnnotation
        annotation.setTitle(new PdfString("iText"));

        // Setting contents of the PdfLineAnnotation
        annotation.setContents("Hi welcome to Baidu! ");

        // Adding annotation to the page
        page.addAnnotation(annotation);

        // Closing the document
        document.close();

        System.out.println("Annotation added successfully");
    }
} 